import {
  home,
} from '../../../../../api/index'
import {
  formatTime
} from "../../../../../utils/util";
let leftHeight = 0,
  rightHeight = 0; //分别定义左右两边的高度
let query;
let listPagination = {
  index: 0,
  num: 10,
}
const {
  globalData: {
    IMGBASEURL
  }
} = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    keywords: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    IMGBASEURL,
    list: [],
    leftList: [],
    rightList: [],
    listLoadStatus: 0,
    isRenderOver: true,
    activeIndex: 1,
    subtitle: undefined,
    tabs: [{
        text: '全部',
        index: 1,
        val: '',
      },
      {
        text: '室内',
        index: 2,
        val: '室内'
      },
      {
        text: '建筑',
        index: 3,
        val: '建筑'
      },
      {
        text: '景观',
        index: 4,
        val: '景观'
      },
      {
        text: '其他',
        index: 5,
        val: '其他'
      },
    ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 选择二级分类
    clickTab(e) {
      console.log('选择二级分类', e.currentTarget.dataset.val);
      console.log('子组件', this.properties.keywords);
      this.setData({
        activeIndex: e.currentTarget.dataset['index'],
        subtitle: e.currentTarget.dataset.val
      })
      this.loadData(true, this.properties.keywords)
      // console.log(this.data.activeIndex);
    },
    shudLogin() {
      const popup = this.selectComponent('#popupid');
      popup.setData({
        visible: true,
        isNavBar: false
      })
    },
    onReachBottom() {
      if (this.data.listLoadStatus === 0 && this.data.isRenderOver) {
        this.loadData()
      }
    },
    onReTry() {
      this.loadData(true);
    },
    //获取左右两边高度
    getBoxHeight(leftList, rightList) {
      return new Promise((resolve, reject) => {
        this.setData({
          leftList,
          rightList
        }, () => {
          query.select('#left').boundingClientRect();
          query.select('#right').boundingClientRect();
          query.exec((res) => {
            setTimeout(() => {
              leftHeight = res[0].height; //获取左边列表的高度
              rightHeight = res[1].height; //获取右边列表的高度
              resolve();
            }, 0)
          });
        });
      })
    },
    // 获取列表
    async loadData(fresh = false, KeyWord = 'a') {
      // console.log('KeyWord', KeyWord);
      this.setData({
        listLoadStatus: 1
      });
      const pageSize = listPagination.num;
      let pageIndex = listPagination.index + 1;
      let nextList = []
      if (fresh) {
        wx.pageScrollTo({
          scrollTop: 0,
        });
        pageIndex = 1;
        this.setData({
          leftList: [],
          rightList: []
        })
        this.setData({
          isShowHistory: false
        })
      }
      try {
        // const res = await home.getallbykeyword({
        //   "PageIndex": pageIndex, //第几页
        //   "PageSize": pageSize, //每页数据量
        //   "Keywords": KeyWord,
        //   "LoadingTime": formatTime(new Date(), 'YYYY-MM-DD')
        // })
        const res = await home.getArticleList({
          "PageIndex": pageIndex, //第几页
          "PageSize": pageSize, //每页数据量
          "keyword": this.properties.keywords,
          "subtitle": this.data.subtitle
        })
        nextList = res.Result.Datas
        this.setData({
          list: fresh ? nextList : this.data?.list.concat(nextList),
        });
        this.setData({
          listLoadStatus: this.data.list?.length == res.Result.TotalCount ? 2 : 0,
        });
        const {
          leftList,
          rightList
        } = this.data;
        query = wx.createSelectorQuery().in(this)
        for (let index = 0; index < nextList.length; index++) {
          if (index == 0) {
            this.data.isRenderOver = false
          }
          const item = nextList[index];
          if (this.data.listLoadStatus == 1) {
            this.data.isRenderOver = true
            return
          }
          if (this.data.leftList.length === 0 && this.data.rightList.length === 0) {
            leftList.push(item)
          } else {
            leftHeight <= rightHeight ? leftList.push(item) : rightList.push(item); //判断两边高度，来觉得添加到那边
          }
          await this.getBoxHeight(leftList, rightList);
          if (index == nextList.length - 1) {
            this.data.isRenderOver = true
          }
        }
        wx.stopPullDownRefresh();
        listPagination.index = pageIndex;
        listPagination.num = pageSize;
      } catch (error) {
        // console.log(error);
        this.setData({
          listLoadStatus: 3
        });
      }
    },
  }
})